package series;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sun.jersey.spi.container.servlet.PerSession;

@Component
@PerSession
@Path("/")
public class SeriesResource {

	@Context
	private ServletContext context; //will have connection to H2 database, see web.xml listeners
	
	@Autowired
	private SeriesDAO seriesDAO;
	
	@Autowired
	private SeasonDAO seasonDAO;
	
	@Autowired
	private EpisodeDAO episodeDAO;
	
	@Path("/watching")
	@GET @Produces(MediaType.APPLICATION_JSON)
	public Response watching() {
		Series watching = seriesDAO.watching();
		return Response.status(200).entity(watching).build();
	}
	
	@Path("/{series}/seasons")
	@GET @Produces(MediaType.APPLICATION_JSON)
	public Response seasons() {
		int seriesId = seriesDAO.seriesId("Spartacus");
		Season[] seasons = seasonDAO.seasonsForSeries(seriesId);
		return Response.status(200).entity(seasons).build();
	}
	
	@POST
	@Path("/add")
	public Response add() {
		return Response.ok().build();
	}

	
//	/**
//	 * Directly injected H2
//	 * 
//	 * @return
//	 * @throws SQLException
//	 */
//	@Path("/track")
//	@GET @Produces(MediaType.APPLICATION_JSON)
//	public Track track() throws SQLException {
//		Connection conn = (Connection) context.getAttribute("connection");  //check listenet in web.xml
//		PreparedStatement prepareStatement = conn.prepareStatement("select * from test");
//		ResultSet executeQuery = prepareStatement.executeQuery();
//		executeQuery.first();
//		String string = executeQuery.getString(2);
//		Track track = new Track();
//		track.setTitle("Enter Sandman");
//		track.setSinger(string);
//		return track;
//	}
}

